<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>卡片式轮播图之后台实现 | hishion blog</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/blog/img/favicon.ico">
    <meta name="description" content="web front-end development engineer">
    <meta name="title" content="hishion blog">
    <meta name="keywords" content="小程序,Vue,uni-app,LESS/SCSS,Git">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/blog/assets/css/0.styles.85454e8d.css" as="style"><link rel="preload" href="/blog/assets/js/app.17d93a93.js" as="script"><link rel="preload" href="/blog/assets/js/2.d5810a70.js" as="script"><link rel="preload" href="/blog/assets/js/37.282a8534.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.a2c8b9d7.js"><link rel="prefetch" href="/blog/assets/js/11.6316d0fd.js"><link rel="prefetch" href="/blog/assets/js/12.927e97fe.js"><link rel="prefetch" href="/blog/assets/js/13.5647b041.js"><link rel="prefetch" href="/blog/assets/js/14.2ae9363a.js"><link rel="prefetch" href="/blog/assets/js/15.00f0b12c.js"><link rel="prefetch" href="/blog/assets/js/16.cf70fbc0.js"><link rel="prefetch" href="/blog/assets/js/17.03f65a2a.js"><link rel="prefetch" href="/blog/assets/js/18.eab9c888.js"><link rel="prefetch" href="/blog/assets/js/19.41e2fb9c.js"><link rel="prefetch" href="/blog/assets/js/20.a81ff0d7.js"><link rel="prefetch" href="/blog/assets/js/21.8f4e6ef9.js"><link rel="prefetch" href="/blog/assets/js/22.33273307.js"><link rel="prefetch" href="/blog/assets/js/23.b9ccad42.js"><link rel="prefetch" href="/blog/assets/js/24.925b6f8f.js"><link rel="prefetch" href="/blog/assets/js/25.09121221.js"><link rel="prefetch" href="/blog/assets/js/26.93b4179f.js"><link rel="prefetch" href="/blog/assets/js/27.248627a6.js"><link rel="prefetch" href="/blog/assets/js/28.a76943a2.js"><link rel="prefetch" href="/blog/assets/js/29.56872daa.js"><link rel="prefetch" href="/blog/assets/js/3.c69e6cb7.js"><link rel="prefetch" href="/blog/assets/js/30.175b2658.js"><link rel="prefetch" href="/blog/assets/js/31.d027670c.js"><link rel="prefetch" href="/blog/assets/js/32.040794e0.js"><link rel="prefetch" href="/blog/assets/js/33.dd9eea2e.js"><link rel="prefetch" href="/blog/assets/js/34.2b31e291.js"><link rel="prefetch" href="/blog/assets/js/35.009bbce8.js"><link rel="prefetch" href="/blog/assets/js/36.ecb36989.js"><link rel="prefetch" href="/blog/assets/js/38.e29d954d.js"><link rel="prefetch" href="/blog/assets/js/39.e071deb9.js"><link rel="prefetch" href="/blog/assets/js/4.20ff38eb.js"><link rel="prefetch" href="/blog/assets/js/40.e30e040e.js"><link rel="prefetch" href="/blog/assets/js/41.f9beaf4c.js"><link rel="prefetch" href="/blog/assets/js/42.631fa7df.js"><link rel="prefetch" href="/blog/assets/js/43.de052ada.js"><link rel="prefetch" href="/blog/assets/js/44.b9d90c8b.js"><link rel="prefetch" href="/blog/assets/js/45.9edc6da4.js"><link rel="prefetch" href="/blog/assets/js/46.43221d60.js"><link rel="prefetch" href="/blog/assets/js/47.70bd2046.js"><link rel="prefetch" href="/blog/assets/js/48.f2da250c.js"><link rel="prefetch" href="/blog/assets/js/49.db6466ad.js"><link rel="prefetch" href="/blog/assets/js/5.413bc4a0.js"><link rel="prefetch" href="/blog/assets/js/50.465c1401.js"><link rel="prefetch" href="/blog/assets/js/51.25711c0b.js"><link rel="prefetch" href="/blog/assets/js/52.c3dd4c01.js"><link rel="prefetch" href="/blog/assets/js/53.c398474b.js"><link rel="prefetch" href="/blog/assets/js/54.1ff939f2.js"><link rel="prefetch" href="/blog/assets/js/55.d00053e3.js"><link rel="prefetch" href="/blog/assets/js/56.4978060f.js"><link rel="prefetch" href="/blog/assets/js/57.b9b8c161.js"><link rel="prefetch" href="/blog/assets/js/58.d67ad12f.js"><link rel="prefetch" href="/blog/assets/js/59.3a1f358e.js"><link rel="prefetch" href="/blog/assets/js/6.747c84d7.js"><link rel="prefetch" href="/blog/assets/js/60.7b70acc9.js"><link rel="prefetch" href="/blog/assets/js/61.43c54fc8.js"><link rel="prefetch" href="/blog/assets/js/62.d27744a0.js"><link rel="prefetch" href="/blog/assets/js/63.73378bfc.js"><link rel="prefetch" href="/blog/assets/js/64.060c996e.js"><link rel="prefetch" href="/blog/assets/js/65.747c9a84.js"><link rel="prefetch" href="/blog/assets/js/66.2a577ef9.js"><link rel="prefetch" href="/blog/assets/js/67.94620d6a.js"><link rel="prefetch" href="/blog/assets/js/68.3cf29532.js"><link rel="prefetch" href="/blog/assets/js/69.9e992128.js"><link rel="prefetch" href="/blog/assets/js/7.7a113c37.js"><link rel="prefetch" href="/blog/assets/js/70.0bf5d79a.js"><link rel="prefetch" href="/blog/assets/js/71.3c940d64.js"><link rel="prefetch" href="/blog/assets/js/72.b39fd551.js"><link rel="prefetch" href="/blog/assets/js/73.b22cb822.js"><link rel="prefetch" href="/blog/assets/js/74.f06bc4d1.js"><link rel="prefetch" href="/blog/assets/js/75.b7bdb0f0.js"><link rel="prefetch" href="/blog/assets/js/76.1dab014e.js"><link rel="prefetch" href="/blog/assets/js/77.7edba438.js"><link rel="prefetch" href="/blog/assets/js/8.eb2c369b.js"><link rel="prefetch" href="/blog/assets/js/9.8d45fb33.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.85454e8d.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><!----> <span class="site-name">hishion blog</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link">
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文章" class="dropdown-title"><span class="title">文章</span> <span class="arrow down"></span></button> <button type="button" aria-label="文章" class="mobile-dropdown-title"><span class="title">文章</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/basic/" class="nav-link">
  前端基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/mp/" class="nav-link">
  小程序
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/" class="nav-link router-link-active">
  Vue
</a></li><li class="dropdown-item"><!----> <a href="/blog/uni/life.html" class="nav-link">
  uni-app
</a></li><li class="dropdown-item"><!----> <a href="/blog/scss/" class="nav-link">
  SCSS/LESS
</a></li><li class="dropdown-item"><!----> <a href="/blog/git/" class="nav-link">
  Git
</a></li><li class="dropdown-item"><!----> <a href="/blog/standard/" class="nav-link">
  前端编码规范
</a></li><li class="dropdown-item"><!----> <a href="/blog/tool/" class="nav-link">
  工具
</a></li><li class="dropdown-item"><!----> <a href="/blog/http/" class="nav-link">
  网络
</a></li><li class="dropdown-item"><!----> <a href="/blog/linux/" class="nav-link">
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/blog/graphql/" class="nav-link">
  GraphQL
</a></li><li class="dropdown-item"><!----> <a href="/blog/other/" class="nav-link">
  其他
</a></li></ul></div></div><div class="nav-item"><a href="https://gitee.com/hishion.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://github.com/weixisheng" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link">
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="文章" class="dropdown-title"><span class="title">文章</span> <span class="arrow down"></span></button> <button type="button" aria-label="文章" class="mobile-dropdown-title"><span class="title">文章</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/blog/basic/" class="nav-link">
  前端基础
</a></li><li class="dropdown-item"><!----> <a href="/blog/mp/" class="nav-link">
  小程序
</a></li><li class="dropdown-item"><!----> <a href="/blog/vue/" class="nav-link router-link-active">
  Vue
</a></li><li class="dropdown-item"><!----> <a href="/blog/uni/life.html" class="nav-link">
  uni-app
</a></li><li class="dropdown-item"><!----> <a href="/blog/scss/" class="nav-link">
  SCSS/LESS
</a></li><li class="dropdown-item"><!----> <a href="/blog/git/" class="nav-link">
  Git
</a></li><li class="dropdown-item"><!----> <a href="/blog/standard/" class="nav-link">
  前端编码规范
</a></li><li class="dropdown-item"><!----> <a href="/blog/tool/" class="nav-link">
  工具
</a></li><li class="dropdown-item"><!----> <a href="/blog/http/" class="nav-link">
  网络
</a></li><li class="dropdown-item"><!----> <a href="/blog/linux/" class="nav-link">
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/blog/graphql/" class="nav-link">
  GraphQL
</a></li><li class="dropdown-item"><!----> <a href="/blog/other/" class="nav-link">
  其他
</a></li></ul></div></div><div class="nav-item"><a href="https://gitee.com/hishion.com" target="_blank" rel="noopener noreferrer" class="nav-link external">
  Gitee
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div><div class="nav-item"><a href="https://github.com/weixisheng" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端基础</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>小程序</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>Vue</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/vue/" aria-current="page" class="sidebar-link">Vue原理解析</a></li><li><a href="/blog/vue/guide.html" class="sidebar-link">Vue规范指南</a></li><li><a href="/blog/vue/auto.html" class="sidebar-link">自动加载文件</a></li><li><a href="/blog/vue/slot.html" class="sidebar-link">Vue插槽</a></li><li><a href="/blog/vue/optimizing.html" class="sidebar-link">Vue优化</a></li><li><a href="/blog/vue/table.html" class="sidebar-link">Vue Element table二次封装</a></li><li><a href="/blog/vue/swiper.html" aria-current="page" class="active sidebar-link">卡片式轮播图之后台实现</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/vue/swiper.html#使用" class="sidebar-link">使用</a></li><li class="sidebar-sub-header"><a href="/blog/vue/swiper.html#效果" class="sidebar-link">效果</a></li><li class="sidebar-sub-header"><a href="/blog/vue/swiper.html#注意" class="sidebar-link">注意</a></li></ul></li><li><a href="/blog/vue/eslint.html" class="sidebar-link">Vue项目配置eslint</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>SCSS/LESS</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Git知识</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>前端规范</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>工具</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Linux知识</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>GraphQL</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>网络知识</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>其他</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="卡片式轮播图之后台实现"><a href="#卡片式轮播图之后台实现" class="header-anchor">#</a> 卡片式轮播图之后台实现</h1> <p>营销活动中，轮播图效果是比较常见的，基于轮播图拓展的组件也很多，这里记录一下使用swiper实现卡片式轮播的过程，分为PC端和小程序端，其中PC端是<code>Vue</code>+<code>typescript</code>，小程序端是<code>uni-app</code>。</p> <div class="custom-block tip"><p class="custom-block-title">小程序说明</p> <p>使用场景为管理后台配置相关参数实现预览效果，小程序根据配置实现相应效果。</p> <p><a href="/blog/uni/swiper.html">卡片式轮播图之小程序实现</a></p></div> <p>使用了 <code>vue-awesome-swiper</code>依赖，首先安装</p> <div class="language-bash line-numbers-mode"><pre class="language-bash"><code><span class="token function">yarn</span> <span class="token function">add</span> swiper vue-awesome-swiper
<span class="token comment"># or</span>
<span class="token function">npm</span> <span class="token function">install</span> swiper vue-awesome-swiper -S
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>vue-awesome-swiper参考文档：<a href="npmjs.com/package/vue-awesome-swiper">vue-awesome-swiper</a></p> <p>swiper的参考文档：<a href="https://www.swiper.com.cn/" target="_blank" rel="noopener noreferrer">swiper中文网<span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></p> <p><code>vue-awesome-swiper</code>中的属性和配置基本同swiper，注意<code>swiper3</code>和<code>swiper≥3</code>区别有点大，具体以安装版本为主。</p> <h2 id="使用"><a href="#使用" class="header-anchor">#</a> 使用</h2> <ol><li>导入组件</li></ol> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">import</span> <span class="token punctuation">{</span> Swiper<span class="token punctuation">,</span> SwiperSlide <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'vue-awesome-swiper'</span>
@<span class="token function">Component</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
  components<span class="token operator">:</span> <span class="token punctuation">{</span>
    Swiper<span class="token punctuation">,</span>
    SwiperSlide
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ol start="2"><li>template基本结构</li></ol> <div class="language-html line-numbers-mode"><pre class="language-html"><code><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Swiper</span>
   <span class="token attr-name">ref</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>myswiper<span class="token punctuation">&quot;</span></span>
   <span class="token attr-name">:options</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>option_swiper<span class="token punctuation">&quot;</span></span>
   <span class="token attr-name">class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>swiper<span class="token punctuation">&quot;</span></span>
   <span class="token attr-name">@slideChange</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>slideChange<span class="token punctuation">&quot;</span></span>
 <span class="token punctuation">&gt;</span></span>
   <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>SwiperSlide</span>
     <span class="token attr-name">v-for</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>(item, index) in images<span class="token punctuation">&quot;</span></span>
     <span class="token attr-name">:key</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>index<span class="token punctuation">&quot;</span></span>
   <span class="token punctuation">&gt;</span></span>
     <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>img</span>
       <span class="token attr-name">:class</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>bem(<span class="token punctuation">'</span>image<span class="token punctuation">'</span>)<span class="token punctuation">&quot;</span></span>
       <span class="token attr-name">:src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>item.ImageUrl<span class="token punctuation">&quot;</span></span>
     <span class="token punctuation">&gt;</span></span>
   <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>SwiperSlide</span><span class="token punctuation">&gt;</span></span>
   <span class="token comment">&lt;!-- 可选 --&gt;</span>
   <span class="token comment">&lt;!-- &lt;div
     slot=&quot;pagination&quot;
     class=&quot;swiper-pagination swiper-pagination-white&quot;
   /&gt; --&gt;</span>
   <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>Pagination</span> <span class="token attr-name">v-if</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>data.Position<span class="token punctuation">&quot;</span></span> <span class="token attr-name">:value</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>images<span class="token punctuation">&quot;</span></span> <span class="token attr-name">:active</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">&quot;</span>activeIndex<span class="token punctuation">&quot;</span></span> <span class="token punctuation">/&gt;</span></span>
 <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>Swiper</span><span class="token punctuation">&gt;</span></span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br></div></div><ol start="3"><li>源数据</li></ol> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token comment">// 图片数据</span>
@<span class="token function">Prop</span><span class="token punctuation">(</span><span class="token punctuation">{</span>
	<span class="token keyword">default</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  		<span class="token keyword">return</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span> images<span class="token operator">!</span><span class="token operator">:</span> any

<span class="token keyword">private</span> option_swiper<span class="token operator">:</span> any <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token comment">// pagination: {</span>
  <span class="token comment">//   el: '.swiper-pagination'</span>
  <span class="token comment">// }, // 这里可选，然后使用自定义指示器</span>
  autoplay<span class="token operator">:</span> <span class="token punctuation">{</span>
    disableOnInteraction<span class="token operator">:</span> <span class="token boolean">false</span>
  <span class="token punctuation">}</span><span class="token punctuation">,</span>
  watchOverflow<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
  loop<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
  observer<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token comment">// 当改变swiper的样式（例如隐藏/显示）或者修改swiper的子元素时，自动初始化swiper</span>
  observeParents<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> <span class="token comment">// 当Swiper的父元素变化时，例如window.resize，Swiper更新</span>
  centeredSlides<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
  spaceBetween<span class="token operator">:</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token comment">// 轮播项间距</span>
  slidesPerView<span class="token operator">:</span> <span class="token number">1.3</span> <span class="token comment">// 用于展示轮播项数量，非整数能露出前后一部分</span>
<span class="token punctuation">}</span>

<span class="token keyword">public</span> activeIndex<span class="token operator">:</span> number <span class="token operator">=</span> <span class="token number">0</span> <span class="token comment">// 可以用来自定义指示器索引</span>

<span class="token keyword">get</span> <span class="token function">myswiper</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token comment">// @ts-ignore</span>
  <span class="token keyword">return</span> <span class="token keyword">this</span><span class="token punctuation">.</span>$refs<span class="token punctuation">.</span>myswiper<span class="token punctuation">.</span>$swiper
<span class="token punctuation">}</span>

<span class="token keyword">public</span> <span class="token function">slideChange</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">this</span><span class="token punctuation">.</span>activeIndex <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>myswiper<span class="token punctuation">.</span>realIndex
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><ol start="4"><li>初始化
由于是组件，数据是通过props传参的，这里最好监听一下数据源，在数据改变时及时变换效果。</li></ol> <div class="language-js line-numbers-mode"><pre class="language-js"><code>@<span class="token function">Watch</span><span class="token punctuation">(</span><span class="token string">'images'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> immediate<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span> deep<span class="token operator">:</span> <span class="token boolean">true</span> <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token function">onImageChanged</span><span class="token punctuation">(</span><span class="token parameter">val<span class="token operator">:</span> any</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">$nextTick</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  <span class="token comment">// 可选</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>val<span class="token punctuation">.</span>length <span class="token operator">&lt;=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>myswiper<span class="token punctuation">.</span>autoplay<span class="token punctuation">.</span><span class="token function">stop</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>myswiper<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>myswiper<span class="token punctuation">.</span><span class="token function">slideToLoop</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span> <span class="token comment">// 在Loop模式下Swiper切换到指定slide</span>
      <span class="token keyword">this</span><span class="token punctuation">.</span>myswiper<span class="token punctuation">.</span>autoplay<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
    <span class="token punctuation">}</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><ol start="5"><li>指示器</li></ol> <p>循环数据，判断active索引高亮即可。</p> <h2 id="效果"><a href="#效果" class="header-anchor">#</a> 效果</h2> <p><img src="/blog/assets/img/swiper-1.639a8dcb.png" alt="卡片式轮播效果"></p> <h2 id="注意"><a href="#注意" class="header-anchor">#</a> 注意</h2> <p>swiper需要以单独组件存在，动态更改option配置不会起作用。即如果有其他配置项，需要新建一个组件，更改<code>option_swiper</code>，例如：</p> <div class="language-js line-numbers-mode"><pre class="language-js"><code><span class="token keyword">private</span> option_swiper<span class="token operator">:</span> any <span class="token operator">=</span> <span class="token punctuation">{</span>
    pagination<span class="token operator">:</span> <span class="token punctuation">{</span>
      el<span class="token operator">:</span> <span class="token string">'.swiper-pagination'</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    autoplay<span class="token operator">:</span> <span class="token punctuation">{</span>
      disableOnInteraction<span class="token operator">:</span> <span class="token boolean">false</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    watchOverflow<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
    loop<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
    observer<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
    observeParents<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
    centeredSlides<span class="token operator">:</span> <span class="token boolean">true</span><span class="token punctuation">,</span>
    spaceBetween<span class="token operator">:</span> <span class="token number">20</span><span class="token punctuation">,</span>
    slidesPerView<span class="token operator">:</span> <span class="token number">1.5</span>
  <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/blog/vue/table.html" class="prev">
        Vue Element table二次封装
      </a></span> <span class="next"><a href="/blog/vue/eslint.html">
        Vue项目配置eslint
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"><!----></div></div>
    <script src="/blog/assets/js/app.17d93a93.js" defer></script><script src="/blog/assets/js/2.d5810a70.js" defer></script><script src="/blog/assets/js/37.282a8534.js" defer></script>
  </body>
</html>
